package com.tj.jinzao.fragment;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.widgets.HorizontalScaleView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 完善资料3-出生年月
 * Created by wjy on 2019/1/6.
 */
public class CompleteInfoStep3Fragment extends BaseFragment {

    @BindView(R.id.scale_view)
    HorizontalScaleView mScaleView;
    @BindView(R.id.tv_year)
    TextView mYearView;
    @BindView(R.id.month_scale)
    HorizontalScaleView mMonthScaleView;
    @BindView(R.id.tv_month)
    TextView mMonthView;
    @BindView(R.id.day_scale)
    HorizontalScaleView mDayScaleView;
    @BindView(R.id.tv_day)
    TextView mDayView;
    @BindView(R.id.ll_date_layout)
    View mDateLayout;
    @BindView(R.id.iv_head)
    ImageView mHeadView;
    @BindView(R.id.tv_sex)
    TextView mSexView;
    @BindView(R.id.time_lay)
    LinearLayout time_lay;
    @BindView(R.id.time_txt)
    TextView time_txt;

    private int mYear = 1960, mMonth = 6, mDay = 16;

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
        int gender = (int) ((CompleteInfoActivity) mContext).getData("gender");
        if (gender == 1) {
            mHeadView.setImageResource(R.mipmap.info_male_head);
            mSexView.setText("男");
        }
        Calendar calendar = Calendar.getInstance();
        mMonth=calendar.get(Calendar.MONTH)+1;
        mDay=calendar.get(Calendar.DAY_OF_MONTH);
        mYear=calendar.get(Calendar.YEAR);
        mYearView.setText(mYear+"年");
        time_txt.setText("请选择日期");
        mScaleView.setRange((mYear-90), (mYear+90));
        mScaleView.setOnValueChangeListener(new HorizontalScaleView.OnValueChangeListener() {
            @Override
            public void onValueChanged(float value) {
                mYear = (int) value;
                mYearView.setText(String.valueOf((int) value) + "年");
                String birthday;
                if(mMonth<10){
                    if(mDay<10){
                        birthday = mYear + "-0" + mMonth + "-0" + mDay;
                    }else {
                        birthday = mYear + "-0" + mMonth + "-" + mDay;
                    }

                }else {
                    if(mDay<10){
                        birthday = mYear + "-" + mMonth + "-0" + mDay;
                    }else {
                        birthday = mYear + "-" + mMonth + "-" + mDay;
                    }
                }
//                time_txt.setText(birthday);

            }
        });

        mMonthScaleView.setRange(1, 12);
        mMonthScaleView.setOnValueChangeListener(new HorizontalScaleView.OnValueChangeListener() {
            @Override
            public void onValueChanged(float value) {
                mMonth = (int) value;
                mMonthView.setText(String.valueOf((int) value) + "月");
            }
        });
        Calendar startDate = Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        time_txt.setText(sdf.format(startDate.getTime()));
        mDayScaleView.setRange(1, 31);
        mDayScaleView.setOnValueChangeListener(new HorizontalScaleView.OnValueChangeListener() {
            @Override
            public void onValueChanged(float value) {
                mDay = (int) value;
                mDayView.setText(String.valueOf((int) value) + "日");
            }
        });
        time_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getTime();
            }
        });
    }
    private TimePickerView pvTime;
    private void getTime(){
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(1960,0,1);
        endDate.set(2100,11,31);
        pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);

                mMonth=calendar.get(Calendar.MONTH)+1;
                mDay=calendar.get(Calendar.DAY_OF_MONTH);
                mYear=calendar.get(Calendar.YEAR);
                String birthday;
                if(mMonth<10){
                    if(mDay<10){
                        birthday = mYear + "-0" + mMonth + "-0" + mDay;
                    }else {
                        birthday = mYear + "-0" + mMonth + "-" + mDay;
                    }

                }else {
                    if(mDay<10){
                        birthday = mYear + "-" + mMonth + "-0" + mDay;
                    }else {
                        birthday = mYear + "-" + mMonth + "-" + mDay;
                    }
                }
                time_txt.setText(birthday);
                mYearView.setText(mYear+"年");
//                mScaleView.setRange((mYear-90), (mYear+90));
//                mScaleView.invalidData();
//                mScaleView.calculateCurrentScale();
//                mScaleView.invalid();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
//                .setCancelText("Cancel")//取消按钮文字
//                .setSubmitText("Sure")//确认按钮文字
//                .setContentSize(18)//滚轮文字大小
//                .setTitleSize(20)//标题文字大小
//                .setTitleText("Title")//标题文字
//                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                .isCyclic(true)//是否循环滚动
//                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.GRAY)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
//                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
//                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setLabel("年","月","日","","","")//默认设置为年月日时分秒
//                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.show();

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_complete_info_step_3;
    }

    @OnClick(R.id.bt_next)
    public void next() {
        if (mContext instanceof CompleteInfoActivity) {
            if (mYear != 0 || mMonth != 0 || mDay != 0) {
                String birthday = mYear + "-" + mMonth + "-" + mDay;
                ((CompleteInfoActivity) mContext).setData("birthday", time_txt.getText().toString());
            }
            ((CompleteInfoActivity) mContext).next();
        }
    }

}
